/*
** Copyright (C) 2009 Joshua Brent Marsh <joshua@icub3d.com>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**  
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**  
*/

/* $Id: test_vector.c 32 2009-10-28 22:51:19Z joshua@icub3d.com $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>

#include <CUnit/Basic.h>

#include "../src/vector.h"

#include "test_helpers.h"
#include "test_vector.h"

Vector *vector;

int
init_test_vector_suite ()
{
  if ((vector = vector_create (25)) == NULL)
    return 1;
  else
    return 0;
}

int
clean_test_vector_suite ()
{
  vector_destroy (vector, &destroy_int);

  return 0;
}

void
test_vector_put ()
{
  int *cur, *ret;

  cur = create_int (5);

  CU_ASSERT (vector_size (vector) == 0);
  CU_ASSERT (vector_max (vector) == 0);
  CU_ASSERT (vector_put (vector, 5, cur) == cur);

  cur = create_int (3);
  CU_ASSERT (vector_put (vector, 3, cur) == cur);

  cur = create_int (20);
  CU_ASSERT (vector_put (vector, 20, cur) == cur);

  cur = create_int (20);
  ret = vector_put (vector, 20, cur);
  CU_ASSERT (ret != cur);
  free (cur);

  cur = create_int (3);
  ret = vector_put (vector, 3, cur);
  CU_ASSERT (ret != cur);
  free (cur);

  cur = create_int (4);
  CU_ASSERT (vector_put (vector, 4, cur) == cur);

  CU_ASSERT (vector_size (vector) == 4);
  CU_ASSERT (vector_max (vector) == 20);

  cur = create_int (0);
  CU_ASSERT (vector_put_first (vector, cur) == cur);
  CU_ASSERT (vector_size (vector) == 5);

  cur = create_int (20);
  ret = vector_put_last (vector, cur);
  CU_ASSERT (ret != cur);
  free (cur);

  cur = create_int (21);
  CU_ASSERT (vector_push_back (vector, cur) == cur);
  CU_ASSERT (vector_size (vector) == 6);
  CU_ASSERT (vector_max (vector) == 21);
}

void 
test_vector_get ()
{
  CU_ASSERT (vector_size (vector) == 6);
  CU_ASSERT (*(int *)vector_get_first (vector) == 0);
  CU_ASSERT (*(int *)vector_get_last (vector) == 21);

  CU_ASSERT (vector_size (vector) == 6);
  CU_ASSERT (*(int *)vector_get (vector, 0) == 0);
  CU_ASSERT (vector_get (vector, 1) == NULL);
  CU_ASSERT (vector_get (vector, 2) == NULL);

  CU_ASSERT (vector_size (vector) == 6);
  CU_ASSERT (vector_capacity (vector) == 25);
}

void
test_vector_remove ()
{
  int *cur;

  CU_ASSERT (vector_size (vector) == 6);
  CU_ASSERT (* (int *)(cur = vector_remove_first (vector)) == 0);
  free (cur);
  CU_ASSERT (vector_size (vector) == 5);
  CU_ASSERT (vector_max (vector) == 21);
  CU_ASSERT (vector_capacity (vector) == 25);

  CU_ASSERT (* (int *)(cur = vector_remove_last (vector)) == 21);
  free (cur);
  CU_ASSERT (vector_size (vector) == 4);
  CU_ASSERT (vector_max (vector) == 20);
  CU_ASSERT (vector_capacity (vector) == 25);

  CU_ASSERT (* (int *)(cur = vector_remove (vector, 4)) == 4);
  free (cur);
  CU_ASSERT (vector_size (vector) == 3);
  CU_ASSERT (vector_max (vector) == 20);
  CU_ASSERT (vector_capacity (vector) == 25);

  CU_ASSERT ((cur = vector_remove (vector, 19)) == NULL);
  CU_ASSERT (vector_size (vector) == 3);
  CU_ASSERT (vector_max (vector) == 20);
  CU_ASSERT (vector_capacity (vector) == 25);

}

void
test_vector_put2()
{
  int *cur;

  cur = create_int (25);
  CU_ASSERT (vector_put (vector, 25, cur) == cur);
  CU_ASSERT (vector_size (vector) == 4);
  CU_ASSERT (vector_max (vector) == 25);
  CU_ASSERT (vector_capacity (vector) == 26);

  cur = create_int (35);
  CU_ASSERT (vector_put (vector, 35, cur) == cur);
  CU_ASSERT (vector_size (vector) == 5);
  CU_ASSERT (vector_max (vector) == 35);
  CU_ASSERT (vector_capacity (vector) == 36);
}

void
test_vector_get2()
{
    CU_ASSERT (vector_size (vector) == 5);
    CU_ASSERT (vector_max (vector) == 35);
    CU_ASSERT (vector_capacity (vector) == 36);

    CU_ASSERT (*(int *)vector_get (vector, 35) == 35);
    CU_ASSERT (*(int *)vector_get (vector, 25) == 25);
    CU_ASSERT (*(int *)vector_get_last (vector) == 35);
    CU_ASSERT (vector_get_first (vector) == NULL);
    CU_ASSERT (vector_get (vector, 0) == NULL);
    CU_ASSERT (vector_get (vector, 21) == NULL);
    CU_ASSERT (vector_get (vector, 26) == NULL);
    CU_ASSERT (vector_get (vector, 34) == NULL);
    CU_ASSERT (vector_get (vector, 17) == NULL);

    CU_ASSERT (vector_size (vector) == 5);
    CU_ASSERT (vector_max (vector) == 35);
    CU_ASSERT (vector_capacity (vector) == 36);
}

void
test_vector_remove2()
{
  int *cur;

  CU_ASSERT (* (int *)(cur = vector_remove (vector, 35)) == 35);
  free (cur);
  CU_ASSERT (vector_size (vector) == 4);
  CU_ASSERT (vector_max (vector) == 25);
  CU_ASSERT (vector_capacity (vector) == 36);


  CU_ASSERT (* (int *)(cur = vector_remove (vector, 25)) == 25);
  free (cur);
  CU_ASSERT (vector_size (vector) == 3);
  CU_ASSERT (vector_max (vector) == 20);
  CU_ASSERT (vector_capacity (vector) == 36);


}

int
make_vector_suite (CU_pSuite s)
{
  s = CU_add_suite ("Test Vector Suite", 
		    init_test_vector_suite, 
		    clean_test_vector_suite);
  if (s == NULL)
    {
      return CU_get_error();
    }

  if (CU_add_test (s, "test vector_put's", test_vector_put) == NULL ||
      CU_add_test (s, "test vector_get's", test_vector_get) == NULL ||
      CU_add_test (s, "test vector_remove's", test_vector_remove) == NULL ||
      CU_add_test (s, "test vector_put's 2", test_vector_put2) == NULL ||
      CU_add_test (s, "test vector_gets's 2", test_vector_get2) == NULL ||
      CU_add_test (s, "test vector_remove's 2", test_vector_remove2) == NULL
      )
    {
      return CU_get_error();
    }

  return CU_get_error();
}
